import * as appConfig from '../../app-config.js';
var app = getApp()
Page({

  data: {
    telphone:'',
    password:'',
    identifyCode:'',
    loginType:0,
    phoneType:true,
    sendVer:'获取验证码',
    num:'',
    weiXinName:'',
    headPortrait:''
  },

  onLoad: function (options) {
    wx.getStorage({
      key:'memInfo',
      success(res){
        console.log(res)
        wx.login({
          success: function(data) {
            console.log(data)
            if (data.code) {
              let code = {
                weiXinName:'',
                headPortrait:'',
                code:data.code,
                agentAccountId:res.data.id
              }
              console.log(code)
              app.request('wx_app/getOpenId',code,'GET').then(item=>{
                console.log(item)
              })
            } else {

            }
          }
        });
        console.log('用户已登录')
        wx.switchTab({
          url: '../my/purchase/purchase',
        })
      },
      fail(res){
        console.log(res.errMsg+':用户未登录')
      }
    })
  },
  //获取输入框中的账号密码
  bindPhoneInput(e){
    console.log(e.detail.value)
    this.setData({
      telphone:e.detail.value
    })
    if (e.detail.value.length==11) {
      this.setData({
        phoneType:false
      })
    }else{
      this.setData({
        phoneType:true
      })
    }
  },
  bindKeyInput(e){
    this.setData({
      password:e.detail.value
    })
  },
  bindCodeInput(e){
    this.setData({
      identifyCode:e.detail.value
    })
  },
  //获取微信名称
  getName(e){
    this.setData({
      weiXinName:e.detail.userInfo.nickName,
      headPortrait:e.detail.userInfo.avatarUrl,
    })
    console.log(e.detail)
    if (this.data.loginType==0) {
      this.loginByIdentifyCode()
    }else{
      this.loginByPassword()
    }
  },
  //登陆
  loginTap(){


  },
  //密码登录
  loginByPassword(){
    wx.showLoading({
      title:'正在登录'
    })
    const that = this
    let data = {
      phone:this.data.telphone,
      pwd:this.data.password
    }
    app.request('wx_app/login',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        wx.setStorage({
          key:"memInfo",
          data:{
            agentId:res.data.agentId,
            id:res.data.id==null?0:res.data.id,
            gender:res.data.gender,
            username:res.data.username,
            name:res.data.name,
            role:res.data.role,
            isEnable:res.data.isEnable,
            shop:res.data.agentCompanyName,
            isVisitor:res.data.isVisitor,
            vipUserName:res.data.vipUserName,
            phone:res.data.phone,
            vipName:res.data.vipName,
            vipId:res.data.vipId
          }
        })
        wx.login({
          success: function(data) {
            console.log(data)
            if (data.code) {
              let code = {
                weiXinName:that.data.weiXinName,
                headPortrait:that.data.headPortrait,
                code:data.code,
                agentAccountId:res.data.id
              }
              console.log(code)
              app.request('wx_app/getOpenId',code,'GET').then(item=>{
                console.log(item)
              })
            } else {

            }
          }
        });
        wx.switchTab({
          url: '../my/purchase/purchase',
        })
      }else{
        wx.hideLoading()
        app.errorMsg('您输入的的账号密码有误')
      }
    })
  },
  //验证码登录
  loginByIdentifyCode(){
    wx.showLoading({
      title:'正在登录'
    })
    const that = this
    let data = {
      phone:this.data.telphone,
      inviteCode:'',
      identifyCode:this.data.identifyCode
    }
    app.request('wx_app/agent_account/add',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        wx.setStorage({
          key:"memInfo",
          data:{
            agentId:res.data.agentId,
            id:res.data.id==null?0:res.data.id,
            gender:res.data.gender,
            username:res.data.username,
            name:res.data.name,
            role:res.data.role,
            isEnable:res.data.isEnable,
            shop:res.data.agentCompanyName,
            isVisitor:res.data.isVisitor,
            vipUserName:res.data.vipUserName,
            phone:res.data.phone,
            vipName:res.data.vipName,
            vipId:res.data.vipId
          }
        })
        wx.login({
          success: function(data) {
            console.log(data)
            if (data.code) {
              let code = {
                weiXinName:that.data.weiXinName,
                headPortrait:that.data.headPortrait,
                code:data.code,
                agentAccountId:res.data.id
              }
              console.log(code)
              app.request('wx_app/getOpenId',code,'GET').then(item=>{
                console.log(item)
              })
            } else {

            }
          }
        });
        wx.switchTab({
          url: '../my/purchase/purchase',
        })
      }else{
        wx.hideLoading()
        app.errorMsg('您输入的的账号密码有误')
      }
    })
  },
  //发送验证码
  sendVer(){
    if (this.data.sendVer==='获取验证码') {
      if (this.data.telphone=='') {
        app.errorMsg('请输入手机号')
      }else{
        let data = {
          phoneNum:this.data.telphone
        }
        console.log(data)
        app.request('wx_app/sendSmsCode',data,'GET').then(res=>{
          console.log(res)
          if (res.code==0) {
            app.errorMsg(res.message)
          }
        })
        this.setData({
          num:120,
          sendVer:'120s后重新发送'
        })
        let time = setInterval(()=>{
          let num = this.data.num
          num --
          this.setData({
            num,
            sendVer:num+'s后重新发送'
          })

          if(num == 0){
           clearInterval(time)
           this.setData({
            sendVer:'获取验证码'
          })
         }
       },1000)
      }
    }else{

    }
  },

  //登录方式切换
  loginChange(e){
    if (this.data.loginType==0) {
      this.setData({
        loginType:1
      })
    }else{
      this.setData({
        loginType:0
      })
    }
  },
  forgetPassWordTap(event){
    wx.navigateTo({
      url: 'forgetPassWord/forgetPassWord',
    })
  },
  register(){
    wx.navigateTo({
      url:'register/register'
    })
  }
})
